package plus.lgx.textsearcher.searcher.printer;

import plus.lgx.textsearcher.entity.PrintData;

import java.io.PrintStream;

import static plus.lgx.textsearcher.constant.Const.LS;

/**
 * Class name: BuiltinOutputHandler
 *
 * 内置的输出模板
 *
 * Create time: 12/21/22 8:13 PM
 *
 * @author lgx
 * @version 1.0
 */
public class BuiltinOutputHandler extends AbstractOutputHandler {

    private String newLinePrefix = "　　 | ";

    public BuiltinOutputHandler(PrintData pd, PrintStream out) {
        super(pd, out);
    }

    @Override
    public void printHead() {

    }

    @Override
    public void printTail() {
        out.format(LS + "TOTAL: %d matched text%s in %d file%s. " + LS, data.totalItemNumber,
                data.totalItemNumber > 1 ? "s" : "", data.fileNumber, data.fileNumber > 1 ? "s" : "");
        out.close();
    }

    @Override
    public void printFileHead() {
        out.print(" " + data.fileNumber);
        out.append(". 【").append((data.fileName.endsWith(".txt")
                        ? data.fileName.substring(0, data.fileName.length() - 4)
                        : data.fileName).trim()).append("】 ")
                .append(data.filePath).append(LS);

        out.print("----------");
    }

    @Override
    public void printFileTail() {
        out.println(LS);
    }

    @Override
    public void printItem() {
        switch (data.itemNumber) {
            case 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 ->
                    newLinePrefix = " " + newLinePrefix;
            case 1 -> newLinePrefix = "　　 | ";
        }
        out.format(LS + "　　%d. %s", data.itemNumber, data.item.replaceAll(LS, LS + newLinePrefix));
    }

    @Override
    public void close() {
        out.close();
    }
}
